<?php

class response extends xpfw {

    private $answer, $controller;

    public function getAnswer() {
        return $this->answer;
    }

    public function setAnswer($answer) {
        $this->answer = $answer;
    }

    public function getController() {
        return $this->controller;
    }

    public function setController($controller) {
        $this->controller = $controller;
    }

    public function __construct($controller, $methodName) {
        $this->setAnswer($controller->$methodName());
        $this->setController($controller);
    }

    public function answer() {
        try {
            if ($answer = $this->getAnswer())
                foreach ($answer as $responseVariable => $responseData)
                    ${$responseVariable} = $responseData;

            $view = $this->getController()->getViewPath();
            $layout = $this->getController()->getLayout();
            $user = $this->getUser();

            if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
                require $view;
            else
                require viewPath . $layout . '.php';
        } catch (Exception $e) {
            $this->log('Error [' . $e->getMessage() . ']', 500);
        }
    }

}
